/*
 * @Author: xiguan
 * @Email: xiguan.teng@qq.com
 * @Version: 1.0
 * @Date: 2022-10-11 16:26:33
 * @LastEditTime: 2022-10-11 16:26:50
 */
#include <bits/stdc++.h>
#include <stdlib.h>

using namespace std;
typedef long long ll;

template<class T>
class linkNode{
public:
    T data_;
    linkNode *next_;

public:
    linkNode() : next_(nullptr) {}
    linkNode( T d ) : data_(d), next_(nullptr) {}
    linkNode( T d, linkNode *ne ) : data_(d), next_(ne) {}
    ~linkNode(){ next_ = nullptr; }
};

template<class T>
class List{
private:
    linkNode<T> *head_;
    int length_;

public:
    List() : length_(0){
        head_ = new linkNode<T>();
    }
    ~List();

public:
    int length();
    bool empty();
    void clear();
    T get( int idx );
    int find( T x );
    bool insert( T x, int idx = -1 );
    bool erase( int idx = -1 );
    void print();
};

template<class T>
List<T>::~List(){
    linkNode<T> *t, *p;
    p = head_;
    while( p != nullptr ){
        t = p;
        p = p->next_;
        delete t;
    }
}

template <class T>
int List<T>::length(){
    return length_;
}

template <class T>
bool List<T>::empty(){
    return length();
}

template <class T>
void List<T>::clear(){
    this->~List();
    head_ = new linkNode<T>();
    length_ = 0;
}

template <class T>
T List<T>::get( int idx ){
    if( idx > length_ || idx <= 0){
        return nullptr;
    }
    int cnt = 0;
    linkNode<T> *p = head_;

    while( p != nullptr ){
        cnt ++;
        if( cnt == idx ){
            return p->data_;
        }
        p = p->next_;
    }

    return nullptr;
}

template <class T>
int List<T>::find( T x ){
    int cnt = 0;
    linkNode<T> *p = head_;

    while( p != nullptr ){
        cnt ++;
        if( p->data_ == x ){
            return cnt;
        }
        p = p->next_;
    }
    return -1;
}

template <class T>
bool List<T>::insert( T x, int idx ){
    if( idx <= 0 || idx > length_ ){
        idx = length_;
    } 
    int cnt = 0;
    linkNode<T> *p = head_, *t;

    if( length_ == 0 ){
        head_->data_ = x;
        length_ ++;
        return true;
    }

    while( p != nullptr ){
        if( ++ cnt == idx )
            break;
        
        p = p->next_;
    }


    t = new linkNode<T>( x, p->next_ );
    p->next_ = t;
    
    length_ ++;
    return true;
}

template <class T>
bool List<T>::erase( int idx ){
    if( idx <= 0 || idx > length_ ){
        idx = length_;
    }
    int cnt = 0;
    linkNode<T> *p = head_, *t = nullptr;

    while( p != nullptr ){
        if( ++ cnt == idx ){
            break;
        }
        t = p;
        p = p->next_;
    }

    if( t != nullptr ){
        t->next_ = p->next_;
        delete p;
    }else{
        t = p->next_;
        head_ = t;
        delete p;
    }
    length_ --;
    return true;
}

template <class T>
void List<T>::print(){
    linkNode<T> *p;
    p = head_;
    while( p != nullptr ){
        cout << p->data_ << " ";
        p = p->next_;
    }
    cout << endl;
}


int main( int argc, char *argv[] ){

    List<int> l;
    l.insert( 2 );
    l.insert( 3 );
    l.insert( 34 );
    l.insert( 71 );
    l.erase( 1 );
    l.erase();
    cout << l.length() << endl;
    l.print();

    return 0;
}